package com.wannengbang.cloudleader.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.DeviceUnifyListBean;
import com.wannengbang.cloudleader.homepage.MachinesInfoActivity;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 合伙人列表
 */
public class MerchantInfoListAdapter extends RecyclerView.Adapter<MerchantInfoListAdapter.ViewHolder> {

    private List<DeviceUnifyListBean.DataBean.ItemListBean> listsBeans;
    private Context context;

    public MerchantInfoListAdapter(List<DeviceUnifyListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_merchant_info_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DeviceUnifyListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvDeviceUnifyNo.setText("设备编号：" + dataBean.getNo());
        holder.tvMerchantNo.setText("商户编号：" + dataBean.getMem_no());
        if (!TextUtils.isEmpty(dataBean.getMem_name())) {
            String name = dataBean.getMem_name().substring(0, dataBean.getMem_name().length() - 1) + "*";
            holder.tvName.setText("商户名称：" + name);
        } else {
            holder.tvName.setText("商户名称：" + "");
        }
        holder.tvMobile.setText("手机号码：" + dataBean.getMem_phone());
        if (TextUtils.isEmpty(dataBean.getEnable_time()) || "0".equals(dataBean.getEnable_time())) {
            holder.tvCreateTime.setText("激活时间：");
        } else {
            holder.tvCreateTime.setText("激活时间：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getEnable_time()) * 1000)));
        }
        String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getStore_order_fee()) / 100);
        holder.tvOrderFee.setText("交易总金额：" + money + "元");
        if (dataBean.getIs_ditto() == 1) {
            holder.tvIsDitto.setVisibility(View.GONE);
        } else {
            holder.tvIsDitto.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, MachinesInfoActivity.class);
            intent.putExtra("id", dataBean.getId() + "");
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_device_unify_no)
        TextView tvDeviceUnifyNo;
        @BindView(R.id.tv_merchant_no)
        TextView tvMerchantNo;
        @BindView(R.id.tv_mobile)
        TextView tvMobile;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.tv_order_fee)
        TextView tvOrderFee;
        @BindView(R.id.tv_is_ditto)
        TextView tvIsDitto;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
